<?php

namespace app\api\model;

use think\Model;

class ImFriend extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:删除好友
     * User: tangfen
     * Date: 2023/12/21
     * Time: 21:27
     * @param $params
     */
    public function delFriend($uid, $touid)
    {
        $result = $this->where(['uid' => $uid, 'touid' => $touid])->update(['is_delete' => 2, 'delete_time' => time()]);
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Notes:好友列表
     * User: tangfen
     * Date: 2023/12/21
     * Time: 21:35
     * @param $where
     * @param $params
     */
    public function friendList($uid, $params)
    {
        if (isset($params['group_id']) && !empty($params['group_id'])) {
            $where = [
                'f.friend_group_id' => $params['group_id'],
                'f.uid' => $uid,
                'f.status' => 2,  //1待审核，2审核通过，3决绝通过
                'f.is_delete' => 1  //1否，2是
            ];
        } else {
            $where = [
                'f.uid' => $uid,
                'f.status' => 2,  //1待审核，2审核通过，3决绝通过
                'f.is_delete' => 1  //1否，2是
            ];
        }

        $list = ImFriend::alias('f')
            ->join('user u', 'u.id=f.touid', 'LEFT')
            ->where($where)
            ->field('u.*, f.id as f_id')
            ->order('f.id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return $list;
    }

    /**
     * Notes:添加好友
     * User: tangfen
     * Date: 2023/12/22
     * Time: 12:08
     * @param $params
     */
    public function addFriend($uid, $touid, $friend_group_id)
    {
        $result = $this->where(['uid' => $uid, 'touid' => $touid, 'friend_group_id' => $friend_group_id])->value('id');
        if (empty($result)) {
            //不存在好友时添加记录
            $this->data([
                'uid' => $uid,
                'touid' => $touid,
                'friend_group_id' => $friend_group_id,
            ]);
            $result = $this->save();
            if ($result) {
                return true;
            } else {
                return false;
            }
        } else {
            $this->where(['uid' => $uid, 'touid' => $touid])->update(['is_delete' => 1]);//存在好友更新删除状态为开启
            return true;
        }
    }

    /**
     * Notes:编辑用户分组
     * User: tangfen
     * Date: 2024/1/3
     * Time: 17:17
     * @param $touid
     * @param $friend_group_id
     */
    public function updateUserGroup($params)
    {
        $result = $this->where(['uid' => $params['uid'], 'touid' => $params['touid']])->update(['friend_group_id' => $params['friend_group_id']]);
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**移除用户分组
     * Notes:
     * User: tangfen
     * Date: 2024/1/3
     * Time: 17:38
     */
    public function delUserGroup($params)
    {
        $result = $this->where(['uid' => $params['uid'], 'touid' => $params['touid']])->update(['friend_group_id' =>'']);
        if ($result !== false) {
            return true;
        } else {
            return false;
        }

    }

}